var api = require("../../api/api.js");
var storageManage = require("../../manage/storageManage.js");
var utils = require("../../utils/utils.js");
var event = require("../../event/event.js");

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentType: "",
    orderList: [],
    orderHave: true,
    balance: 0,
    mPageNum: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      currentType: options.id
    });

    event.on('paySuccess', this, function (data) {
      that.reset(data)
    });
    this.getList();

  },
  reset: function (data) {
    this.setData({
      orderList: [],
      mPageNum: 1,
      currentType: data.order_status
    })
    this.getList();
  },
  onReady: function () {
    //获得modalContainer组件
    this.modalContainer = this.selectComponent("#modalPay");
  },
  showModalPay: function (e) {
    var price = e.currentTarget.dataset.price;
    var order_no = e.currentTarget.id;
    var payload = {
      order_no: order_no,
      balance: storageManage.getGlobalConfig().balance,
      commodity_cost: price
    }
    this.modalContainer.showModalContainer(payload);
  },
  _pay: function (e) {
    //支付成功返回支付方式的状态值
    var paymentState = e.detail.paymentState;
    var par = {};

    if (paymentState == 0) {
      par.order_status = "pending_send"

    } else if (paymentState == 3) {
      par.order_status = "pending_send"

    } else {
      par.order_status = "pending_check"
    }
    this.reset(par);
    this.modalContainer.hideModal();

  },
  //tab切换
  seleTab: utils.throttle(function (e) {
    this.setData({
      currentType: e.currentTarget.id,
      mPageNum: 1,
      orderList: [],
      orderHave: true
    });
    this.getList();
  }),

  //下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.data.orderList = []
    this.data.mPageNum = 1;
    this.getList();
  },

  //上拉加载
  onReachBottom: function () {
    this.setData({
      isHidden: false
    });
    this.data.mPageNum += 1;
    this.getList()
  },
  //订单详情
  toOrderDetail: utils.throttle(function (e) {
    console.log(e)
    wx.navigateTo({
      url: '../orderDetail/orderDetail?order_id=' + e.currentTarget.dataset.order_id

    })
  }),
  //获取列表
  getList: function () {
    var that = this;
    wx.showLoading()
    api.default.api('get_order_list', {
      "status": this.data.currentType,
      "page": this.data.mPageNum
    }, function (data) {
      wx.hideLoading()
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({
        isHidden: true
      });
      if (data.data.length == 0) { //没有数据且是第一页

        if (that.data.mPageNum <= 1) {
          that.setData({
            orderHave: false
          })
        } else {
          that.data.mPageNum -= 1;
        }
        return;
      }
      that.setData({
        orderList: that.data.orderList.concat(data.data)
      });
      wx.hideNavigationBarLoading()

    }, function () {
      wx.stopPullDownRefresh() //停止下拉刷新
      wx.hideNavigationBarLoading()
      wx.hideLoading()
    });
  },
  //去支付
  goPayTap: utils.throttle(function (e) {
    console.log(e.currentTarget.id)
  }),
  //取消订单
  cancelOrderTap: utils.throttle(function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认取消该订单',
      success: function (res) {
        if (res.confirm) {
          var orderList = that.data.orderList;
          var index = e.currentTarget.dataset.index;
          api.default.api('cancel_order', { "order_no": e.currentTarget.id }, function (res) {
            wx.showToast({
              title: '取消订单成功',
            });
            let newData = that.data.orderList;
            newData.splice(index, 1);
            that.setData({
              orderList: newData
            })
          }, function (e, du) {
            wx.showToast({
              title: '取消订单失败' + JSON.stringify(e),
            })
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  }),

  // 确认收货
  affirmTap: utils.throttle(function (e) {
    var that = this;

    wx.showModal({
      title: '提示',
      content: '确认收到货物',
      success: function (res) {
        if (res.confirm) {
          var index = e.currentTarget.dataset.index;
          var newData = that.data.orderList;
          api.default.api('affirm_recv', { "order_no": e.currentTarget.id }, function (res) {
            wx.showToast({
              title: '确认收到货物',
            });
            newData.splice(index, 1);
            that.setData({
              orderList: newData
            })
          }, function (e, du) {
            wx.showToast({
              title: '确认收货失败' + JSON.stringify(e),
            })
          });


        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  }),
  //查看物流
  logistics: utils.throttle(function (e) {
    // e.currentTarget.id
    wx.navigateTo({
      url: '../logistics/logistics?order_id=' + e.currentTarget.id
    })

  })


})